<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>

<table id="payers_table" class="table table-bordered">
</table>
<script type="text/javascript">
    $(function () {
         $('#payers_table').datagrid({
            height: '100%',
             width:'100%',
             fullGrid:true,
             columnMenu: false,
             contextMenuH:false,
             fieldSortable:false,
            gridTitle: '第三方支付',
            showToolbar: true,
            toolbarCustom:function(){
                return '<button type="button" class="btn btn-blue" onclick="savePayer();" data-icon="plus"><i class="fa fa-plus"></i> 添加</button>';
            },
            toolbarItem:"refresh,|,del",
            filterThead: false,
            delPK: 'id',
            dataUrl: '/admin/payer/payers',
            local: "local",
            inlineEditMult: false,
            saveAll: false,
            addLocation: 'last',
            editUrl: '/admin/payer/save',
            delUrl: '/admin/payer/delete',
            keys: {_id:"_id",addTime:"addTime",submitUrl:"submitUrl",receiveUrl:"receiveUrl",name:"name",nid:"nid",status:"status",type:"type",id:"id"},
            delConfirm:true,
            delCallback: function (response) {
                if (response.haveError) {
                    BJUI.alertmsg('error', response.errMsg, {
                        okCall: function () {
                            BJUI.navtab('refresh')
                        }
                    });
                } else {
                    BJUI.alertmsg('ok', response.data, {
                        okCall: function () {
                            BJUI.navtab('refresh')
                        }
                    });
                }
            },
            columns: [
                {
                    name: 'name',
                    label: '名称',
                    align: 'center',
                    rule:  'required',
                    add:false
                }, {
                    name: 'type',
                    label: '类型',
                    align: 'center',
                    edit:false,
                    add:false,
                    items: [{0: '托管'}, {1: '非托管'}],
                    rule:  'required'
                }, {
                    name: 'nid',
                    label: '标识',
                    align: 'center',
                    rule:  'required',
                    add:false
                },{
                    name: 'submitUrl',
                    label: '提交信息url',
                    align: 'center',
                    add:false
                },{
                    name: 'receiveUrl',
                    label: '接收信息url',
                    align: 'center'
                },{
                    name: 'receivePageUrl',
                    label: '接收页面信息url',
                    align: 'center'
                }, {
                    render: payerStatusRender,
                    label: '状态',
                    align: 'center'
                }, {
                    render: operation,
                    label: '操作列'
                }
            ],
            paging: {pageSize: 20, pageIndex: 1},
            linenumberAll: true
        });

    });

    /**
     * 操作返回函数
     * @param value：
     * @param data：
     * @returns {string}
     */
    function operation(value, data) {
        var html = '<button type="button" class="btn-green" onclick="savePayer(\''+data.id+'\');">编辑</button>'
            + '<button type="button" class="btn-red" data-toggle="del.datagrid.tr">删除</button>';
        return html
    }

    /**
     * 状态列返回函数
     */
    function payerStatusRender(value, data) {
        if (data.status) {//开状态
            return '<div data-status="false" data-val = "' + data.id + '" class="selectBtn" onclick="updatePayerStatus(this);"><div class="openWord">开</div><div class="open1 select_01"><div class="open2 select_02"></div></div><div class="closeWord">关</div></div>';
        } else {
            return '<div data-status="true" data-val = "' + data.id + '" class="selectBtn" onclick="updatePayerStatus(this);"><div class="openWord">开</div><div class="open1 select_01 close1"><div class="select_02"></div></div><div class="closeWord">关</div></div>';
        }
    }

    /**
     * 状态操作
     */
    function updatePayerStatus(e) {
        var $this = $(e);
        var id = $this.data("val");
        var status = $this.data("status");
        if (id === null || id === "" || typeof (id) === "undefined" || status === null || status === "" || typeof (status) === "undefined") {
            BJUI.alertmsg('error', "参数错误，重新操作", {
                okCall: function () {
                    BJUI.navtab('refresh')//刷新当前窗口
                }
            });
            return;
        }

        BJUI.ajax('doajax', {
            url: '/admin/payer/updateStatus',
            loadingmask: true,
            data: {"id": id, "status": status},
            callback: function (response, options) {
                if (response.haveError) {
                    BJUI.alertmsg('error', response.errMsg, {
                        okCall: function () {
                            $('#payers_table').datagrid("refresh",true);
                        }
                    });

                } else {
                    BJUI.alertmsg('ok', response.data, {
                        okCall: function () {
                            $('#payers_table').datagrid("refresh",true);
                        }
                    });
                }
            }
        });
    }

    /**
     * 保存payer窗口
     * @param id：
     */
    function savePayer(id){
        var url = "/admin/payer/savePage";
        if(id!==""&&id!==null&&typeof (id) !== "undefined" ){
            url = url + "?id="+id;
        }
        BJUI.dialog({
            id: 'save_payer',
            url: url,
            title: '编辑',
            resizable: true,
            width: 575,
            height: 400,
            fresh:true
        });
    }
</script>